using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Xml.XPath;
using System.Xml;

namespace MessengerQuotePlugin
{
    public class Item
    {
        private DateTime dateTime;
        private string message;
        private string from;
        private string to;
        private Color color;
        private int session;

        public Item(XPathNavigator node)
        {
            try
            {
                dateTime = XmlConvert.ToDateTime(node.SelectSingleNode("@DateTime").TypedValue as string, XmlDateTimeSerializationMode.Local);
                from = node.SelectSingleNode("./From/User/@FriendlyName").TypedValue as string;
                to = node.SelectSingleNode("./To/User/@FriendlyName").TypedValue as string;
                message = node.SelectSingleNode("./Text").TypedValue as string;
                session = XmlConvert.ToInt32(node.SelectSingleNode("@SessionID").TypedValue as string);

                XPathNavigator styleNode = node.SelectSingleNode("./Text/@Style");
                if (styleNode != null)
                {
                    SetStyleProperties(styleNode.TypedValue as string);
                }
            }
            catch (Exception ex)
            {
                from = "MessengerQuotePlugin";
                message = "Error while processing Messenger history: " + ex.Message;
            }
        }

        private void SetStyleProperties(string style)
        {
            Match match = Regex.Match(style, "(?!color:)#[0-9A-F]+", RegexOptions.IgnoreCase);
            if (match.Success)
            {
                color = ColorTranslator.FromHtml(match.Value);
            }
            else
            {
                color = SystemColors.WindowText;
            }
        }

        public DateTime DateTime
        {
            get { return dateTime; }
            private set { dateTime = value; }
        }

        public string From
        {
            get { return from; }
            private set { from = value; }
        }

        public string To
        {
            get { return to; }
            private set { to = value; }
        }

        public string Message
        {
            get { return message; }
            private set { message = value; }
        }

        public Color Color
        {
            get { return color; }
            private set { color = value; }
        }

        public int Session
        {
            get { return session; }
            private set { session = value; }
        }
    }
}